﻿using LView.CMS.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Text;
using System.Security.Claims;
using System.Linq;
using LView.CMS.Core.Models;
using MongoDB.Bson;
using LView.CMS.Core.Helper;

namespace LView.CMS.Site.Controllers
{
    [Authorize]
    public abstract class BaseController : Controller
    {
        protected string ToErrorString(ModelStateDictionary modelState, string split)
        {
            if (split.IsNullOrEmpty())
            {
                split = "||";
            }
            StringBuilder errinfo = new StringBuilder();
            foreach (var s in ModelState.Values)
            {
                foreach (var p in s.Errors)
                {
                    errinfo.AppendFormat("{0}{1}", p.ErrorMessage, split);
                }
            }
            if (errinfo.Length > split.Length)
            {
                errinfo.Remove(errinfo.Length - 2, split.Length);
            }
            return errinfo.ToString();
        }

        public static Operator CurrentUser { get; set; }

        public IActionResult Success<T>(T t)
        {
            return Ok(JsonHelper.ObjectToJson(t));
        }
        public IActionResult Fail(string msg)
        {
            return Ok(new { errorCode = -1, msg });
        }
    }
}
